<?php
	require_once("theme-functions.php");
	/*!
	 * \brief Creates breadcrumbs path.
	 * \param homeTitle The title of homepage if front page is blog posts.
	 * \param separator The character(s) for separating parts of text.
	 * \returns whole breadcrumbs path.
	 */
	function awisp_breadcrumbs($homeTitle, $separator){
		global $post;
		if(!$post){
			return null;
		}
		if($post->post_parent > 0){
			$path = awisp_breadcrumbs_get_item($post->post_parent, $homeTitle, $separator) . $separator . $post->post_title;
		}
		else{
			$path = awisp_breadcrumbs_get_link(awisp_get_home_page($homeTitle)) . $separator . $post->post_title;
		}
		return $path;
	}
	/*!
	 * \brief Creates breadcrumbs recursively.
	 * \param postId The id of the post.
	 * \param homeTitle The title of homepage if front page is blog posts.
	 * \param separator The character(s) for separating parts of text.
	 * \returns breadcrums separated by the separator.
	 */
	function awisp_breadcrumbs_get_item($postId, $homeTitle, $separator){
		$post = get_post($postId);
		if($post->post_parent > 0){
			return awisp_breadcrumbs_get_item($post->post_parent, $separator) . $separator . awisp_breadcrumbs_get_link($post);
		}
		else{
			return awisp_breadcrumbs_get_link(awisp_get_home_page($homeTitle)) . $separator . awisp_breadcrumbs_get_link($post);
		}
	}
	
	/*!
	 * \brief Creates clickable link to post.
	 * \param post The post to link.
	 * \returns clickable link to post.
	 */
	function awisp_breadcrumbs_get_link($post){
		if($post->ID == 0){
			$permalink = home_url("/");
		}
		else{
			$permalink = get_permalink($post->ID);
		}
		return '<a href="' . esc_url($permalink) . '">' . $post->post_title . '</a>';
	}
	/*!
	 * \brief Creates copyright info in a form of years interval.
	 * \param estabilishedYear The year in which the website was estabilished.
	 * \returns copyright info.
	 */
	function awisp_copyright_info($estabilishedYear){
		$currentYear = date("Y");
		if($currentYear == $estabilishedYear){
			return $estabilishedYear;
		}
		else{
			return $estabilishedYear . '&ndash;' . $currentYear;
		}
	}
	/*!
	 * \brief Creates clickable post edit link.
	 * \returns clickable post edit link.
	 */
	function awisp_edit_post_link(){
		$link = get_edit_post_link();
		if(!empty($link)){
			echo '<div id="entry-edit"><p><a href="' . $link . '">' . __('Edit', "awisp.theme") . '</a></p></div>';
		}
		return null;
	}
	/*!
	 * \brief Creates custom excerpt format.
	 * \param text Placeholder argument.
	 * \param raw_excerpt Placeholder argument.
	 * \returns content of excerpt.
	 */
	function awisp_excerpt($text, $raw_excerpt){
		global $post;
		if(!$raw_excerpt){
			$content = apply_filters('the_content', strip_shortcodes(get_the_content()));
			/* 4 means length of </p> */
			$text = substr($content, 0, strpos($content, '</p>') + 4);
			$text = preg_replace("/<img[^>]+\>/i", "", $text);
			$text .= '<p><a href="' . get_permalink($post->ID) . '">' . __("Read more", "awisp.theme") . '</a></p>';
		}
		return $text;
	}
	add_filter("wp_trim_excerpt", "awisp_excerpt", 10, 2);
	/*!
	 * \brief Gets page set as front.
	 * \param homeTitle The title of homepage if front page is blog posts.
	 * \returns page set as front.
	 */
	function awisp_get_home_page($homeTitle){
		if(get_option("show_on_front") == "page"){
			return get_post(get_option("page_on_front"));
		}
		else{
			$homepage = new stdClass();
			$homepage->ID = 0;
			$homepage->post_title = $homeTitle;
			return $homepage;
		}
	}
	/*!
	 * \brief Redefines the default post password protection form.
	 * \returns password protection form.
	 */
	function awisp_post_password_form(){
		global $post;
		$id = "pwbox-" . (empty($post->ID)? rand(): $post->ID);
		$output = '<div id="password-protection-form">';
		$output .= '<form action="' . esc_url(site_url("wp-login.php?action=postpass", "login_post")) . '" method="post">';
		$output .= '<p>' . __('This post is password protected. To view it please enter your password below.', "awisp.theme").'</p>';
		$output .= '<p id="password-protection-form-p"><label for="' . $id . '">' . __("Password", "awisp.theme") . '</label><input type="password" id="' . $id . '" name="post_password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__("Submit", "awisp.theme") . '" /></p>';
		$output .= '</form>';
		$output .= '</div>';
		return $output;
	}
	add_filter("the_password_form", "awisp_post_password_form");
	/*!
	 * \brief Redefines generating content of the title HTML tag.
	 * \param title Placeholder argument.
	 * \param separator The character for separating parts of text.
	 * \param separatorLocation Separator location {left, right}.
	 * \returns content of title HTML tag.
	 */
	function awisp_title_generator($title, $separator = '&raquo;', $separatorLocation){
		global $paged, $page, $wp_locale, $post;
		$blogName = get_bloginfo("name");
		$separator = " " . trim($separator) . " ";
		/* Pagination */
		if(($paged >= 2) || ($page >= 2)){
			$title = sprintf(__("Page %s", "awisp.theme"), max($paged, $page)) . $separator;
		}
		else{
			$title = null;
		}
		/* Blogname */
		if(!empty($blogName)){
			$title .= $blogName;
		}
		else{
			$title .= $_SERVER["SERVER_NAME"];
		}
		/* Homepage */
		if(is_home() || is_front_page()){
			$siteDescription = get_bloginfo("description");
			if(!empty($siteDescription)){
				$title .= $separator . $siteDescription;
			}
			elseif(is_front_page()){
				$frontPageId = get_option("page_on_front");
				if($frontPageId > 0){
					$frontPageTitle = get_the_title($frontPageId);
					$title = $frontPageTitle . $separator . $title;
				}
			}
		}
		/* Single post, Page with posts, Static page */
		if(is_single() || (is_home() && !is_front_page()) || (is_page() && !is_front_page())){
			$parentID = $post->post_parent;
			if($parentID > 0){
				$parentTitle = get_the_title($parentID);
				$title = $parentTitle . $separator . $title;
			}
			$postTitle = single_post_title(null, false);
			if(!empty($postTitle)){
				$title = $postTitle . $separator . $title;
			}
		}
		/* Category */
		if(is_category()){
			$title = single_term_title(null, false) . $separator . $title;
		}
		/* Tag */
		if(is_tag()){
			$title = __("Tag: ", "awisp.theme") . single_term_title(null, false) . $separator . $title;
		}
		/* Taxonomy */
		if(is_tax()){
			$term = get_queried_object();
			$tax = get_taxonomy($term->taxonomy);
			$title = single_term_title($tax->labels->name, false) . $separator . $title;
		}
		/* Author */
		if(is_author()){
			$author = get_queried_object();
			$title =  sprintf(__("Author: %s", "awisp.theme"), $author->display_name) . $separator . $title;
		}
		/* Post Type Archive */
		if(is_post_type_archive()){
			$title = post_type_archive_title(null, false) . $separator . $title;
		}
		/* Date based archive */
		if(is_archive() && is_date()){
			$year = get_query_var("year");
			if(!empty($year)){
				$month = get_query_var("monthnum");
				if(!empty($month)){
					$day = get_query_var("day");
					if(!empty($day)){
						$title = $wp_locale->get_month($month) . " " . $day . ", " . $year  . $separator . $title;
					}
					else{
						$title = $wp_locale->get_month($month) . " " . $year  . $separator . $title;
					}
				}
				else{
					$title = sprintf(__("Year %s", "awisp.theme"), $year)  . $separator . $title;
				}
			}
			else{
				$title = __("Archive", "awisp.theme")  . $separator . $title;
			}
		}
		/* Search results page */
		if(is_search()){
			$title = __("Search results", "awisp.theme") . $separator . $title;
		}
		/* 404 page */
		if(is_404()){
			$title = __("Page not found", "awisp.theme") . $separator . $title;
		}
		/* Separator location */
		if($separatorLocation != "right"){
			$titleArray = explode($separator, $title);
			$titleArray = array_reverse($titleArray);
			$title = implode($separator, $titleArray);
		}
		return $title;
	}
	add_filter("wp_title", "awisp_title_generator", 10, 3);
?>